home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / demos / gears_stats.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-15  |  16.9 KB  |  883 lines

  1. #include <mgl/gl.h>
  2. //#include <mgl/mglmacros_norms.h>
  3.  
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>    //OF
  8.  
  9. //#include <StAnDebug.h>
  10.  
  11. #ifdef __VBCC__
  12. #include <extra.h>    //OF
  13. #pragma amiga-align
  14. #endif
  15.  
  16. #include <exec/exec.h>
  17. #include <intuition/intuition.h>
  18. #include <graphics/gfx.h>
  19. #include <libraries/lowlevel.h>
  20. #include <dos/dos.h>
  21.  
  22. #ifdef __VBCC__
  23. #pragma default-align
  24. #endif
  25.  
  26. #ifdef __GNUC__
  27.     #ifdef __PPC__
  28.     #include <proto/exec.h>
  29.     #include <proto/intuition.h>
  30.     #include <proto/lowlevel.h>
  31.     #include <proto/dos.h>
  32.     #include <proto/graphics.h>
  33.     #else
  34.     #include <inline/exec.h>
  35.     #include <inline/intuition.h>
  36.     #include <inline/lowlevel.h>
  37.     #include <inline/dos.h>
  38.     #include <inline/graphics.h>
  39.     #endif
  40. #endif
  41.  
  42. #ifdef __STORMC__
  43.     #include <clib/powerpc_protos.h>
  44.     #ifndef __PPC__
  45.     #include <proto/exec.h>
  46.     #include <proto/intuition.h>
  47.     #include <proto/lowlevel.h>
  48.     #include <proto/dos.h>
  49.     #include <proto/graphics.h>
  50.     #else
  51.     #include <clib/exec_protos.h>
  52.     #include <clib/intuition_protos.h>
  53.     #include <clib/lowlevel_protos.h>
  54.     #include <clib/dos_protos.h>
  55.     #include <clib/graphics_protos.h>
  56.     #endif
  57. #endif
  58.  
  59. #ifdef __VBCC__
  60. #pragma amiga-align
  61.  
  62. #include <proto/exec.h>
  63. #include <proto/intuition.h>
  64. #include <proto/lowlevel.h>
  65. #include <proto/dos.h>
  66. #include <proto/graphics.h>
  67.  
  68. #pragma default-align
  69. #endif
  70.  
  71.  
  72. /* Some <math.h> files do not define M_PI... */
  73. #ifndef M_PI
  74. #define M_PI 3.14159265
  75. #endif
  76.  
  77. /* For portability... */
  78. #undef fcos
  79. #undef fsin
  80. #undef fsqrt
  81. //#ifndef TRIGTABLES
  82. #define fcos  cos
  83. #define fsin  sin
  84. /*
  85. #else
  86. #include "mgl/sincos.h"
  87. #define fcos  MGL_RADCOS
  88. #define fsin  MGL_RADSIN
  89. #endif
  90. */
  91. #define fsqrt sqrt
  92.  
  93. static double d_near = 1.0;
  94. static double d_far = 2000;
  95. static int poo = 0;
  96.  
  97. int show = 1;
  98.  
  99. GLint width=640; GLint height=480;
  100.  
  101. typedef struct {
  102.   float rad, wid;
  103. } Profile;
  104.  
  105. void flat_face(float ir, float or, float wd);
  106. void draw_inside(float w1, float w2, float rad);
  107. void draw_outside(float w1, float w2, float rad);
  108. void tooth_side(int nt, float ir, float or, float tp, float tip, float wd);
  109.  
  110. int circle_subdiv;
  111. #define MIN_SUBDIV 30
  112.  
  113. GLboolean bEnvMap = GL_TRUE;
  114.  
  115. static GLboolean culling = GL_FALSE;
  116. static GLboolean clockwise = GL_TRUE;
  117. static ULONG tris = 0;
  118.  
  119. GLubyte *LoadPPM(char *name, GLint *w, GLint *h)
  120. {
  121. //    PF( "LoadPPM( %p %s, %p, %p )\n", (void*)name, name?name:"***", (void*)w, (void*)h );
  122.     int i;
  123.     unsigned long x,y;
  124.     FILE *f;
  125.     GLubyte *where;
  126.  
  127.     f = fopen(name, "r");
  128.  
  129.     if (!f)
  130.     {
  131.         *w = 0; *h=0;
  132.         return NULL;
  133.     }
  134.     #ifndef __STORM__
  135.     i = fscanf(f, "P6\n%lu %lu\n255\n",&x, &y);    //OF (%lu)
  136.     #else
  137.     i = fscanf(f, "P6\n%lu\n%lu\n255\n", &x, &y);    //OF (%lu)
  138.     #endif
  139.  
  140.     if (i!= 2)
  141.     {
  142.         printf("Error scanning PPM header\n");
  143.         fclose(f);
  144.         *w = 0; *h = 0;
  145.         return NULL;
  146.     }
  147.  
  148.     *w = x;
  149.     *h = y;
  150.  
  151.     where = malloc(x*y*3);
  152.     if (!where)
  153.     {
  154.         printf("Error out of Memory\n");
  155.         fclose(f);
  156.         *w = 0; *h = 0;
  157.         return NULL;
  158.     }
  159.  
  160.     i = fread(where, 1, x*y*3, f);
  161.     fclose(f);
  162.  
  163.     if (i != x*y*3)
  164.     {
  165.         printf("Error while reading file\n");
  166.         free(where);
  167.         *w = 0; *h = 0;
  168.         return NULL;
  169.     }
  170.  
  171.     return where;
  172. }
  173.  
  174.  
  175. BOOL TexInit(char *name)
  176. {
  177. //    PF("TexInit( %p %s )\n", (void*)name, name?name:"***" );
  178.     GLubyte *tmap;
  179.     GLint x,y;
  180.  
  181.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  182.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  183.  
  184.     if (!name)
  185.     {
  186.         tmap = LoadPPM("data/chrome.ppm",&x, &y);
  187.     }
  188.     else
  189.     {
  190.         tmap = LoadPPM(name, &x, &y);
  191.     }
  192.  
  193.     if (!tmap)
  194.         return FALSE;
  195.  
  196. //    PUTS("b4 glBindTexture");
  197.     glBindTexture(GL_TEXTURE_2D, 1);
  198. //    PF( "b4 glTexImage2D x=%d y=%d tmp=%p\n", x, y, (void*)tmap );
  199.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  200.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  201. //    PUTS("b4 free(tmap)");
  202.     free(tmap);
  203.  
  204. //    PUTS("b4 Texparam");
  205.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  206.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  207.  
  208. //    PUTS("b4 glEnable");
  209.     glEnable(GL_TEXTURE_2D);
  210.     glEnable(GL_TEXTURE_GEN_S);
  211.     glEnable(GL_TEXTURE_GEN_T);
  212. //    PUTS("b4 glTexGeni");
  213.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  214.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  215.  
  216.     return TRUE;
  217. }
  218.  
  219.  
  220.  
  221. void
  222. gear(int nt, float wd, float ir, float or, float tp, float tip, int ns, Profile * ip)
  223. {
  224.   /**
  225.    * nt - number of teeth 
  226.    * wd - width of gear at teeth
  227.    * ir - inside radius absolute scale
  228.    * or - radius at outside of wheel (tip of tooth) ratio of ir
  229.    * tp - ratio of tooth in slice of circle (0..1] (1 = teeth are touching at base)
  230.    * tip - ratio of tip of tooth (0..tp] (cant be wider that base of tooth)
  231.    * ns - number of elements in wheel width profile
  232.    * *ip - list of float pairs {start radius, width, ...} (width is ratio to wd)
  233.    *
  234.    */
  235.  
  236.   /* gear lying on xy plane, z for width. all normals calulated 
  237.      (normalized) */
  238.  
  239.   float prev;
  240.   int k, t;
  241.  
  242.   /* estimat # times to divide circle */
  243.   if (nt <= 0)
  244.     circle_subdiv = MIN_SUBDIV;
  245.   else {
  246.     /* lowest multiple of number of teeth */
  247.     circle_subdiv = nt;
  248.     while (circle_subdiv < MIN_SUBDIV)
  249.       circle_subdiv += nt;
  250.   }
  251.  
  252.   /* --- draw wheel face --- */
  253.  
  254.   /* draw horzontal, vertical faces for each section. if first
  255.      section radius not zero, use wd for 0.. first if ns == 0
  256.      use wd for whole face. last width used to edge.  */
  257.  
  258.   if (ns <= 0) {
  259.     flat_face(0.0, ir, wd);
  260.   } else {
  261.     /* draw first flat_face, then continue in loop */
  262.     if (ip[0].rad > 0.0) {
  263.       flat_face(0.0, ip[0].rad * ir, wd);
  264.       prev = wd;
  265.       t = 0;
  266.     } else {
  267.       flat_face(0.0, ip[1].rad * ir, ip[0].wid * wd);
  268.       prev = ip[0].wid;
  269.       t = 1;
  270.     }
  271.     for (k = t; k < ns; k++) {
  272.       if (prev < ip[k].wid) {
  273.         draw_inside(prev * wd, ip[k].wid * wd, ip[k].rad * ir);
  274.       } else {
  275.         draw_outside(prev * wd, ip[k].wid * wd, ip[k].rad * ir);
  276.       }
  277.       prev = ip[k].wid;
  278.       /* - draw to edge of wheel, add final face if needed - */
  279.       if (k == ns - 1) {
  280.         flat_face(ip[k].rad * ir, ir, ip[k].wid * wd);
  281.  
  282.         /* now draw side to match tooth rim */
  283.         if (ip[k].wid < 1.0) {
  284.           draw_inside(ip[k].wid * wd, wd, ir);
  285.         } else {
  286.           draw_outside(ip[k].wid * wd, wd, ir);
  287.         }
  288.       } else {
  289.         flat_face(ip[k].rad * ir, ip[k + 1].rad * ir, ip[k].wid * wd);
  290.       }
  291.     }
  292.   }
  293.  
  294.   /* --- tooth side faces --- */
  295.   tooth_side(nt, ir, or, tp, tip, wd);
  296.  
  297.   /* --- tooth hill surface --- */
  298. }
  299.  
  300. void 
  301. tooth_side(int nt, float ir, float or, float tp, float tip, float wd)
  302. {
  303.  
  304.   float i;
  305.   float end = 2.0 * M_PI / nt;
  306.   float x[6], y[6];
  307.   float s[3], c[3];
  308.  
  309.   or = or * ir;         /* or is really a ratio of ir */
  310.   for (i = 0; i < 2.0 * M_PI - end / 4.0; i += end) {
  311.  
  312.     c[0] = fcos(i);
  313.     s[0] = fsin(i);
  314.     c[1] = fcos(i + end * (0.5 - tip / 2));
  315.     s[1] = fsin(i + end * (0.5 - tip / 2));
  316.     c[2] = fcos(i + end * (0.5 + tp / 2));
  317.     s[2] = fsin(i + end * (0.5 + tp / 2));
  318.  
  319.     x[0] = ir * c[0];
  320.     y[0] = ir * s[0];
  321.     x[5] = ir * fcos(i + end);
  322.     y[5] = ir * fsin(i + end);
  323.     /* ---treat veritices 1,4 special to match strait edge of
  324.        face */
  325.     x[1] = x[0] + (x[5] - x[0]) * (0.5 - tp / 2);
  326.     y[1] = y[0] + (y[5] - y[0]) * (0.5 - tp / 2);
  327.     x[4] = x[0] + (x[5] - x[0]) * (0.5 + tp / 2);
  328.     y[4] = y[0] + (y[5] - y[0]) * (0.5 + tp / 2);
  329.     x[2] = or * fcos(i + end * (0.5 - tip / 2));
  330.     y[2] = or * fsin(i + end * (0.5 - tip / 2));
  331.     x[3] = or * fcos(i + end * (0.5 + tip / 2));
  332.     y[3] = or * fsin(i + end * (0.5 + tip / 2));
  333.  
  334.     /* draw face trapezoids as 2 tmesh */
  335.     glNormal3f(0.0, 0.0, 1.0);
  336.     glBegin(GL_TRIANGLE_STRIP);
  337.     glVertex3f(x[2], y[2], wd / 2);
  338.     glVertex3f(x[1], y[1], wd / 2);
  339.     glVertex3f(x[3], y[3], wd / 2);
  340.     glVertex3f(x[4], y[4], wd / 2);
  341.     glEnd();
  342.  
  343.     tris +=2;
  344.  
  345.     glNormal3f(0.0, 0.0, -1.0);
  346.     glBegin(GL_TRIANGLE_STRIP);
  347.     glVertex3f(x[2], y[2], -wd / 2);
  348.     glVertex3f(x[1], y[1], -wd / 2);
  349.     glVertex3f(x[3], y[3], -wd / 2);
  350.     glVertex3f(x[4], y[4], -wd / 2);
  351.     glEnd();
  352.  
  353.     tris +=2;
  354.  
  355.     /* draw inside rim pieces */
  356.     glNormal3f(c[0], s[0], 0.0);
  357.     glBegin(GL_TRIANGLE_STRIP);
  358.     glVertex3f(x[0], y[0], -wd / 2);
  359.     glVertex3f(x[1], y[1], -wd / 2);
  360.     glVertex3f(x[0], y[0], wd / 2);
  361.     glVertex3f(x[1], y[1], wd / 2);
  362.     glEnd();
  363.  
  364.     tris +=2;
  365.  
  366.     /* draw up hill side */
  367.     {
  368.       float a, b, n;
  369.       /* calculate normal of face */
  370.       a = x[2] - x[1];
  371.       b = y[2] - y[1];
  372.       n = 1.0 / fsqrt(a * a + b * b);
  373.       a = a * n;
  374.       b = b * n;
  375.       glNormal3f(b, -a, 0.0);
  376.     }
  377.     glBegin(GL_TRIANGLE_STRIP);
  378.     glVertex3f(x[1], y[1], -wd / 2);
  379.     glVertex3f(x[2], y[2], -wd / 2);
  380.     glVertex3f(x[1], y[1], wd / 2);
  381.     glVertex3f(x[2], y[2], wd / 2);
  382.     glEnd();
  383.  
  384.     tris +=2;
  385.  
  386.     /* draw top of hill */
  387.     glNormal3f(c[1], s[1], 0.0);
  388.     glBegin(GL_TRIANGLE_STRIP);
  389.     glVertex3f(x[2], y[2], -wd / 2);
  390.     glVertex3f(x[3], y[3], -wd / 2);
  391.     glVertex3f(x[2], y[2], wd / 2);
  392.     glVertex3f(x[3], y[3], wd / 2);
  393.     glEnd();
  394.  
  395.     tris +=2;
  396.  
  397.     /* draw down hill side */
  398.     {
  399.       float a, b, c;
  400.       /* calculate normal of face */
  401.       a = x[4] - x[3];
  402.       b = y[4] - y[3];
  403.       c = 1.0 / fsqrt(a * a + b * b);
  404.       a = a * c;
  405.       b = b * c;
  406.       glNormal3f(b, -a, 0.0);
  407.     }
  408.     glBegin(GL_TRIANGLE_STRIP);
  409.     glVertex3f(x[3], y[3], -wd / 2);
  410.     glVertex3f(x[4], y[4], -wd / 2);
  411.     glVertex3f(x[3], y[3], wd / 2);
  412.     glVertex3f(x[4], y[4], wd / 2);
  413.     glEnd();
  414.  
  415.     tris +=2;
  416.  
  417.     /* inside rim part */
  418.     glNormal3f(c[2], s[2], 0.0);
  419.     glBegin(GL_TRIANGLE_STRIP);
  420.     glVertex3f(x[4], y[4], -wd / 2);
  421.     glVertex3f(x[5], y[5], -wd / 2);
  422.     glVertex3f(x[4], y[4], wd / 2);
  423.     glVertex3f(x[5], y[5], wd / 2);
  424.     glEnd();
  425.     tris +=2;
  426.  
  427.   }
  428. }
  429.  
  430. void 
  431. flat_face(float ir, float or, float wd)
  432. {
  433.  
  434.   int i;
  435.   float w;
  436.  
  437.   /* draw each face (top & bottom ) * */
  438.   if (poo)
  439.     printf("Face  : %f..%f wid=%f\n", ir, or, wd);
  440.   if (wd == 0.0)
  441.     return;
  442.   for (w = wd / 2; w > -wd; w -= wd) {
  443.     if (w > 0.0)
  444.     {
  445.       glNormal3f(0.0, 0.0, 1.0);
  446.     }
  447.     else
  448.     {
  449.       glNormal3f(0.0, 0.0, -1.0);
  450.     }
  451.  
  452.     if (ir == 0.0) {
  453.       /* draw as t-fan */
  454.       glBegin(GL_TRIANGLE_FAN);
  455.       glVertex3f(0.0, 0.0, w);  /* center */
  456.       glVertex3f(or, 0.0, w);
  457.  
  458.       for (i = 1; i < circle_subdiv; i++) {
  459.         glVertex3f(fcos(2.0 * M_PI * i / (float)circle_subdiv) * or,
  460.           fsin(2.0 * M_PI * i / (float)circle_subdiv) * or,
  461.           w);
  462.       }
  463.       glVertex3f(or, 0.0, w);
  464.       glEnd();
  465.     tris +=circle_subdiv;
  466.  
  467.     } else {
  468.       /* draw as tmesh */
  469.       glBegin(GL_TRIANGLE_STRIP);
  470.       glVertex3f(or, 0.0, w);
  471.       glVertex3f(ir, 0.0, w);
  472.       for (i = 1; i < circle_subdiv; i++) {
  473.         glVertex3f(fcos(2.0 * M_PI * i / (float)circle_subdiv) * or,
  474.           fsin(2.0 * M_PI * i / (float)circle_subdiv) * or,
  475.           w);
  476.         glVertex3f(fcos(2.0 * M_PI * i / (float)circle_subdiv) * ir,
  477.           fsin(2.0 * M_PI * i / (float)circle_subdiv) * ir,
  478.           w);
  479.       }
  480.       glVertex3f(or, 0.0, w);
  481.       glVertex3f(ir, 0.0, w);
  482.  
  483.       glEnd();
  484.     tris +=circle_subdiv+1;
  485.  
  486.     }
  487.   }
  488. }
  489.  
  490. void 
  491. draw_inside(float w1, float w2, float rad)
  492. {
  493.  
  494.   int i, j;
  495.   float c, s;
  496.   if (poo)
  497.     printf("Inside: wid=%f..%f rad=%f\n", w1, w2, rad);
  498.   if (w1 == w2)
  499.     return;
  500.  
  501.   w1 = w1 / 2;
  502.   w2 = w2 / 2;
  503.   for (j = 0; j < 2; j++) {
  504.     if (j == 1) {
  505.       w1 = -w1;
  506.       w2 = -w2;
  507.     }
  508.     glBegin(GL_TRIANGLE_STRIP);
  509.     glNormal3f(-1.0, 0.0, 0.0);
  510.     glVertex3f(rad, 0.0, w1);
  511.     glVertex3f(rad, 0.0, w2);
  512.     for (i = 1; i < circle_subdiv; i++) {
  513.       c = fcos(2.0 * M_PI * i / circle_subdiv);
  514.       s = fsin(2.0 * M_PI * i / circle_subdiv);
  515.       glNormal3f(-c, -s, 0.0);
  516.       glVertex3f(c * rad,
  517.         s * rad,
  518.         w1);
  519.       glVertex3f(c * rad,
  520.         s * rad,
  521.         w2);
  522.     }
  523.     glNormal3f(-1.0, 0.0, 0.0);
  524.     glVertex3f(rad, 0.0, w1);
  525.     glVertex3f(rad, 0.0, w2);
  526.     glEnd();
  527.     tris +=circle_subdiv*2;
  528.   }
  529. }
  530.  
  531. void 
  532. draw_outside(float w1, float w2, float rad)
  533. {
  534.  
  535.   int i, j;
  536.   float c, s;
  537.   if (poo)
  538.     printf("Outsid: wid=%f..%f rad=%f\n", w1, w2, rad);
  539.   if (w1 == w2)
  540.     return;
  541.  
  542.   w1 = w1 / 2;
  543.   w2 = w2 / 2;
  544.   for (j = 0; j < 2; j++) {
  545.     if (j == 1) {
  546.       w1 = -w1;
  547.       w2 = -w2;
  548.     }
  549.     glBegin(GL_TRIANGLE_STRIP);
  550.     glNormal3f(1.0, 0.0, 0.0);
  551.     glVertex3f(rad, 0.0, w1);
  552.     glVertex3f(rad, 0.0, w2);
  553.     for (i = 1; i < circle_subdiv; i++) {
  554.       c = fcos(2.0 * M_PI * i / circle_subdiv);
  555.       s = fsin(2.0 * M_PI * i / circle_subdiv);
  556.       glNormal3f(c, s, 0.0);
  557.       glVertex3f(c * rad,
  558.         s * rad,
  559.         w1);
  560.       glVertex3f(c * rad,
  561.         s * rad,
  562.         w2);
  563.     }
  564.     glNormal3f(1.0, 0.0, 0.0);
  565.     glVertex3f(rad, 0.0, w1);
  566.     glVertex3f(rad, 0.0, w2);
  567.     glEnd();
  568.     tris +=circle_subdiv*2;
  569.   }
  570. }
  571.  
  572. Profile gear_profile[] =
  573. {0.000, 0.0,
  574.   0.300, 7.0,
  575.   0.340, 0.4,
  576.   0.550, 0.64,
  577.   0.600, 0.4,
  578.   0.950, 1.0
  579. };
  580.  
  581. float a1 = 27.0;
  582. float a2 = 67.0;
  583. float a3 = 47.0;
  584. float a4 = 87.0;
  585. float i1 = 1.2;
  586. float i2 = 3.1;
  587. float i3 = 2.3;
  588. float i4 = 1.1;
  589.  
  590.  
  591. struct Library *LowLevelBase;
  592.  
  593. #ifndef __PPC__
  594. extern struct IntuitionBase *IntuitionBase;
  595. extern struct GfxBase *GfxBase;
  596. extern struct Library *UtilityBase;
  597. extern struct DosLibrary *DOSBase;
  598. extern struct ExecBase *SysBase;
  599. #endif
  600. static struct EClockVal eval;
  601. static float fps;
  602.  
  603. void PrExit(void)
  604. {
  605.     if (LowLevelBase)   CloseLibrary(LowLevelBase);
  606.     exit(0L);
  607. }
  608.  
  609. void PrInit(void)
  610. {
  611.     LowLevelBase  = OpenLibrary("lowlevel.library", 40L);
  612.     if (!LowLevelBase) PrExit();
  613. }
  614.  
  615.  
  616. void oneFrame(void)
  617. {
  618.     ULONG fracsecs;
  619.     static  char buffer[256];
  620.     struct Window *win = mglGetWindowHandle();
  621.     static int framecount = 0;
  622.     tris = 0;
  623.  
  624.     SetAPen(win->RPort, 2);
  625.  
  626.   mglLockDisplay();
  627.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  628.  
  629.   if (show & 1)
  630.   {
  631.     glPushMatrix();
  632.     glTranslatef(0.0, 0.0, -4.0);
  633.     glRotatef(a3, 1.0, 1.0, 1.0);
  634.     glRotatef(a4, 0.0, 0.0, -1.0);
  635.     glTranslatef(0.14, 0.2, 0.0);
  636.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  637.     gear(40,
  638.         0.4, 2.0, 1.1,
  639.         0.8, 0.4,
  640.         sizeof(gear_profile) / sizeof(Profile), gear_profile);
  641.     glPopMatrix();
  642.   }
  643.  
  644.   if (show & 2)
  645.   {
  646.     glPushMatrix();
  647.     glTranslatef(0.1, 0.2, -3.8);
  648.     glRotatef(a2, -4.0, 2.0, -1.0);
  649.     glRotatef(a1, 1.0, -3.0, 1.0);
  650.     glTranslatef(0.0, -0.2, 0.0);
  651.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  652.     glColor3f(1.0, 0.8, 0.0);
  653.     gear(36,
  654.         0.4, 2.0, 1.1,
  655.         0.7, 0.2,
  656.         sizeof(gear_profile) / sizeof(Profile), gear_profile);
  657.     glPopMatrix();
  658.   }
  659.  
  660.   a1 += i1;
  661.   if (a1 > 360.0)
  662.     a1 -= 360.0;
  663.   if (a1 < 0.0)
  664.     a1 -= 360.0;
  665.   a2 += i2;
  666.   if (a2 > 360.0)
  667.     a2 -= 360.0;
  668.   if (a2 < 0.0)
  669.     a2 -= 360.0;
  670.   a3 += i3;
  671.   if (a3 > 360.0)
  672.     a3 -= 360.0;
  673.   if (a3 < 0.0)
  674.     a3 -= 360.0;
  675.   a4 += i4;
  676.   if (a4 > 360.0)
  677.     a4 -= 360.0;
  678.   if (a4 < 0.0)
  679.     a4 -= 360.0;
  680.  
  681.   mglSwitchDisplay();
  682.  
  683.     framecount++;
  684.  
  685.     fracsecs  = ElapsedTime(&eval);
  686.     fracsecs &= 0xFFFF;
  687.  
  688.     fps = 65536.0/(float)(fracsecs+1);
  689.     
  690.     Move(win->RPort, win->Width - 64, 16);
  691.     sprintf(buffer, "FPS: %4.2f", fps);
  692.     Text(win->RPort, buffer, strlen(buffer));
  693.  
  694.     Move(win->RPort, win->Width - 64, 32);
  695.     sprintf(buffer, "TPS: %4.2f", (float)tris*fps);
  696.     Text(win->RPort, buffer, strlen(buffer));
  697. }
  698.  
  699. #if 0
  700. void
  701. display(void)
  702. {
  703.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  704. }
  705. #endif
  706.  
  707. void
  708. myReshape(int w, int h)
  709. {
  710.   glViewport(0, 0, (GLint)w, (GLint)h);
  711.   glMatrixMode(GL_PROJECTION);
  712.   glLoadIdentity();
  713.   glFrustum(-1.0, 1.0, -1.0, 1.0, d_near, d_far);
  714.   /**
  715.     use perspective instead:
  716.  
  717.     if (w <= h){
  718.         glOrtho( 0.0, 1.0,
  719.                  0.0, 1.0 * (GLfloat) h / (GLfloat) w,
  720.                 -16.0, 4.0);
  721.     }else{
  722.         glOrtho( 0.0, 1.0 * (GLfloat) w / (GLfloat) h,
  723.                  0.0, 1.0,
  724.                 -16.0, 4.0);
  725.     }
  726.    */
  727.   glMatrixMode(GL_MODELVIEW);
  728.   glLoadIdentity();
  729.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  730. }
  731.  
  732.  
  733. void myinit(int w, int h)
  734. {
  735.   float f[20];
  736.   /* glClearColor(0.0, 0.0, 0.0, 0.0); */
  737.   myReshape(w, h);
  738.   /* glShadeModel(GL_FLAT); */
  739.   glEnable(GL_DEPTH_TEST);
  740.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  741.  
  742. }
  743.  
  744. /* ARGSUSED1 */
  745. void
  746. keys(char c)
  747. {
  748.  
  749.   if (c == 0x1b)
  750.     mglExit();
  751.  
  752.   if (c == '1')
  753.     show = 1;
  754.  
  755.   if (c == '2')
  756.     show = 2;
  757.  
  758.   if (c == '3')
  759.     show = 3;
  760.  
  761.   if (c == 'c') //switch culling on/off
  762.   {
  763.     if(culling == GL_TRUE)
  764.     {
  765.         culling = GL_FALSE;
  766.         glDisable(GL_CULL_FACE);
  767.     }
  768.     else
  769.     {
  770.         culling = GL_TRUE;
  771.         glEnable(GL_CULL_FACE);
  772.     }
  773.   }
  774.  
  775.   if (c == 'r') //reverse order
  776.   {
  777.     if(clockwise == GL_TRUE)
  778.     {
  779.         glFrontFace(GL_CCW);
  780.         clockwise = GL_FALSE;
  781.     }
  782.     else
  783.     {
  784.         glFrontFace(GL_CW);
  785.         clockwise = GL_TRUE;
  786.     }
  787.  
  788.   }
  789.  
  790.   if (c == 'e')
  791.   {
  792.     if (bEnvMap == GL_TRUE)
  793.     {
  794.         bEnvMap = GL_FALSE;
  795.         glDisable(GL_TEXTURE_GEN_S);
  796.         glDisable(GL_TEXTURE_GEN_T);
  797.     }
  798.     else
  799.     {
  800.         bEnvMap = GL_TRUE;
  801.         glEnable(GL_TEXTURE_GEN_S);
  802.         glEnable(GL_TEXTURE_GEN_T);
  803.     }
  804.   }
  805. }
  806.  
  807. int main(int argc, char *argv[])
  808. {
  809.     int i;
  810.     char *name = 0;
  811.  
  812.     for (i=1; i<argc; i++)
  813.     {
  814.         if (0 == stricmp(argv[i], "-width"))
  815.         {
  816.             i++;
  817.             width = atoi(argv[i]);
  818.         }
  819.         if (0 == stricmp(argv[i], "-height"))
  820.         {
  821.             i++;
  822.             height = atoi(argv[i]);
  823.         }
  824.         if (0 == stricmp(argv[i], "-envmap"))
  825.         {
  826.             i++;
  827.             name = argv[i];
  828.         }
  829.         if (0 == stricmp(argv[i], "-window"))
  830.         {
  831.             mglChooseWindowMode(GL_TRUE);
  832.         }
  833.     }
  834.  
  835.     mglChooseVertexBufferSize(1000);
  836.     mglChooseNumberOfBuffers(3);
  837.     mglChoosePixelDepth(16);
  838.     PrInit();
  839.     MGLInit();
  840. //    PUTS("mglcre");
  841.     mglCreateContext(0,0,width,height);
  842. //    PUTS("mglenab");
  843.     mglEnableSync(GL_FALSE);
  844. //    PUTS("glhint");
  845.     glHint(MGL_W_ONE_HINT, GL_FASTEST);
  846.  
  847. //    PUTS("texinit");
  848.     if ( TexInit(name) )
  849.     {
  850. //        PUTS("in");
  851.         glClearColor(0.0, 0.0, 0.0, 1.0);
  852.         glColor3f(1.0, 0.0, 0.0);
  853.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  854.  
  855.         glCullFace(GL_FRONT);
  856.         glFrontFace(GL_CW);
  857.  
  858.         glDisable(GL_CULL_FACE);
  859.  
  860.         glHint(MGL_W_ONE_HINT, GL_FASTEST);
  861.  
  862. //        PUTS("myinit");
  863.         myinit(width, height);
  864.  
  865.         mglLockMode(MGL_LOCK_MANUAL);
  866.         mglIdleFunc(oneFrame);
  867.         mglKeyFunc(keys);
  868.         mglMainLoop();
  869.     }
  870.     else
  871.     {
  872.         printf("Can't find texture %s\n", name?name:"data/chrome.ppm"); //OF (name==NULL check)
  873.         printf("Can't find texture %s\n", name);
  874.     }
  875.  
  876.     mglDeleteContext();
  877.     MGLTerm();
  878.       PrExit();
  879.  
  880.     return 0;             /* ANSI C requires main to return int. */
  881. }
  882.  
  883.